from CvPythonExtensions import *

import GerikSpells
from GerikSpells import GerikSpellBase
from GSGroupPolicies import GSAll as GSGroup
from GSHandlerPolicies import GSImmediateNoTarget as GSHandler
from GSResultsPolicies import GSMissionNoTarget as GSResults

import GerikSpellsUtils

from CivcraftSCUtils import checkAndRemoveEnergy
from CivcraftSCUtils import showNotEnoughEnergyMsg
from CivcraftSCUtils import getEnergyUsageString

gc = CyGlobalContext()
localtext = CyTranslator()

class GhostCloak(GerikSpellBase, GSGroup, GSHandler, GSResults):
    def __init__(self):
        GerikSpellBase.__init__(self)
        GSGroup.__init__(self)
        GSHandler.__init__(self)
        GSResults.__init__(self)

        self.m_iGhostUnitType = gc.getInfoTypeForString("UNIT_GHOST")
        self.m_iGhostResearchNeeded = gc.getInfoTypeForString("PROJECT_TERRAN_GHOST_CLOAKING")
        self.m_iGhostResearchBuildingToBuildAt = gc.getProjectInfo(self.m_iGhostResearchNeeded).getBuildingToBuildAt()
        self.m_iCastingCost = gc.getDefineINT("CLOAK_CASTING_COST")
        self.m_szNotValidText = localtext.getText( "TXT_KEY_CIVCRAFT_REQUIRES_RESEARCH_AT", (gc.getProjectInfo(self.m_iGhostResearchNeeded).getDescription(),
                                                                                             gc.getBuildingInfo(self.m_iGhostResearchBuildingToBuildAt).getDescription()) )


    def getHelpText(self, pGroup, pPlot):
        szText = getEnergyUsageString(self.m_iCastingCost)

        bValid = self.canGroupCast(pGroup, pPlot, False)
        if not bValid:
            szText += self.m_szNotValidText
        else:
            szText += gc.getSpellInfo(self.getSpellNumber()).getHelp()

        return szText
            
    def canUnitCast(self, pCastingUnit, pPlot, bVisible):
        # For ghosts only.
        if not pCastingUnit.getUnitType() == self.m_iGhostUnitType:
            return False

        # Don't allow for a unit that is already cloaked.
        if pCastingUnit.isCloakBurningEnergy():
            return False

        # Show this for all ghosts, only enable it if they have the research.
        if bVisible:
            return True

        # Check if they have the research
        if not gc.getPlayer( pCastingUnit.getOwner() ).hasBuiltProject( self.m_iGhostResearchNeeded ):
            return False

        return True
        
    def startSpellResults(self, pCastingGroup, pTarget):
        bShowNotEnoughEnergy = False
        for pUnit in pCastingGroup.getUnits():
            if checkAndRemoveEnergy(pUnit, self.m_iCastingCost):
                pUnit.setCloakBurningEnergy(True)
            else:
                bShowNotEnoughEnergy = True

        if bShowNotEnoughEnergy:
            iPlayer = pCastingGroup.getOwner()
            showNotEnoughEnergyMsg(iPlayer)

        # Don't continue the mission.
        return False

class WraithCloak(GerikSpellBase, GSGroup, GSHandler, GSResults):
    def __init__(self):
        GerikSpellBase.__init__(self)
        GSGroup.__init__(self)
        GSHandler.__init__(self)
        GSResults.__init__(self)

        self.m_iWraithUnitType = gc.getInfoTypeForString("UNIT_WRAITH")
        self.m_iWraithResearchNeeded = gc.getInfoTypeForString("PROJECT_TERRAN_WRAITH_CLOAKING")
        self.m_iWraithResearchBuildingToBuildAt = gc.getProjectInfo(self.m_iWraithResearchNeeded).getBuildingToBuildAt()
        self.m_iCastingCost = gc.getDefineINT("CLOAK_CASTING_COST")
        self.m_szNotValidText = localtext.getText( "TXT_KEY_CIVCRAFT_REQUIRES_RESEARCH_AT", (gc.getProjectInfo(self.m_iWraithResearchNeeded).getDescription(),
                                                                                             gc.getBuildingInfo(self.m_iWraithResearchBuildingToBuildAt).getDescription()) )

    def getHelpText(self, pGroup, pPlot):
        szText = getEnergyUsageString(self.m_iCastingCost)

        bValid = self.canGroupCast(pGroup, pPlot, False)
        if not bValid:
            szText += self.m_szNotValidText
        else:
            szText += gc.getSpellInfo(self.getSpellNumber()).getHelp()

        return szText
            

    def canUnitCast(self, pCastingUnit, pPlot, bVisible):
        # For Wraiths only.
        if not pCastingUnit.getUnitType() == self.m_iWraithUnitType:
            return False

        # Don't allow for a unit that is already cloaked.
        if pCastingUnit.isCloakBurningEnergy():
            return False

        # Show this for all ghosts, only enable it if they have the research.
        if bVisible:
            return True

        # Check if they have the research
        if not gc.getPlayer( pCastingUnit.getOwner() ).hasBuiltProject( self.m_iWraithResearchNeeded ):
            return False

        return True
        
    def startSpellResults(self, pCastingGroup, pTarget):
        bShowNotEnoughEnergy = False
        for pUnit in pCastingGroup.getUnits():
            if checkAndRemoveEnergy(pUnit, self.m_iCastingCost):
                pUnit.setCloakBurningEnergy(True)
            else:
                bShowNotEnoughEnergy = True
                

        if bShowNotEnoughEnergy:
            iPlayer = pCastingGroup.getOwner()
            showNotEnoughEnergyMsg(iPlayer)

        # Don't continue the mission.
        return False

class Decloak(GerikSpellBase, GSGroup, GSHandler, GSResults):
    def __init__(self):
        GerikSpellBase.__init__(self)
        GSGroup.__init__(self)
        GSHandler.__init__(self)
        GSResults.__init__(self)

    def canUnitCast(self, pCastingUnit, pPlot, bVisible):
        if not pCastingUnit.isCloakBurningEnergy():
            return False

        return True
        
    def startSpellResults(self, pCastingGroup, pTarget):
        for pUnit in pCastingGroup.getUnits():
            pUnit.setCloakBurningEnergy(False)

        # Don't continue the mission
        return False


        

                        
            
        
            
            
